﻿using Company.Database.Entities;
using Company.Database.Interfaces;
using Company.Share.Message;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ToolKit.Mvvm.Attributes;
using ToolKit.Mvvm.Bindings;
using ToolKit.Mvvm.Enums;
using ToolKit.Mvvm.Events;

namespace Company.Demo.ViewModels
{
    [DependenceInjection(Lifetime.Singleton)]
    internal class LoginViewModel : ViewModelBase
    {
        private User user = new User() { UserName = "admin", Password = "12345678" };
        public User User { get => user; set => SetProperty(ref user, value); }


        public ICommand LoginCommand { get; }

        private IUserRepository UserRepository { get; }

        public LoginViewModel(IUserRepository userRepository)
        {
            UserRepository = userRepository;
            LoginCommand = new RelayCommand<ContentControl>(Login);
        }

        private void Login(ContentControl obj)
        {
            var entity = UserRepository.GetAll().FirstOrDefault(t => t.UserName == User.UserName && t.Password == User.Password);
            if(entity != null)
            {
                EventBus.Default.Publish(this, new LoginMessage(entity));//登录成功，跳转首页
            }
            else
            {
                MessageBox.Show("用户名或密码错误");
            }
            
        }
    }
}
